package cn.com.api.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import cn.com.api.entity.ApiJuan;
import cn.com.api.entity.ApiT_coupon_act;
import cn.com.api.entity.ApiT_juan_tj;
import cn.com.api.entity.ApiT_recommend_coupon;
import cn.com.api.entity.ApiTmember;
import cn.com.api.mapper.ApiJuanMapper;
import cn.com.api.mapper.ApiJuanTJMapper;
import cn.com.api.mapper.ApiT_coupon_actMapper;
import cn.com.api.mapper.ApiT_recommend_couponMapper;
import cn.com.api.mapper.ApiTmemberMapper;
import cn.com.api.service.ApiT_recommend_couponService;
import cn.com.util.*;
import cn.com.util.*;

@Service
public class ApiT_recommend_couponServiceImpl implements ApiT_recommend_couponService{
	
	@Autowired
	private ApiT_recommend_couponMapper apiT_recommend_couponMapper;
	@Autowired
	private ApiTmemberMapper apiTmemberMapper;
	@Autowired
	private ApiT_coupon_actMapper apiT_coupon_actMapper;
	@Autowired
	private ApiJuanMapper apiJuanMapper;
	@Autowired
	private ApiJuanTJMapper apiJuanTJMapper;
	/**
	 * 校验当前领取的手机号
	 * @param params
	 * @param map
	 * @return
	 */
	@Override
	public boolean checkCurrentMobile(Map<String, String> params,
			Map<String, Object> map) {
		boolean flag = true;
		String recommend_mobile = params.get("recommend_mobile");
		String current_mobile = params.get("current_mobile");
		/**
		 * 1-先判断该手机号current_mobile是否是已注册用户
		 * 2-不是已注册用户，是否领过recommend_mobile推荐的优惠券
		 * 3-没有领过recommend_mobile推荐的优惠券，是否领过他人推荐的优惠券
		 */
		ApiTmember member = apiTmemberMapper.findMemberByCuserid(current_mobile);
		System.out.println("member--------------------------"+member);
		if(member==null){
			ApiT_recommend_coupon recommendCoupon = apiT_recommend_couponMapper.findByRecommendAndCurrent(recommend_mobile, current_mobile);
			if(recommendCoupon==null){
				recommendCoupon = apiT_recommend_couponMapper.findByCurrent(current_mobile);
				if(recommendCoupon==null){
					flag = true;
				}else{
					flag = false;
					map.put(Utils.RESULT, Utils.RESULT_RC_CHECK_FAIL);
					map.put(Utils.MESSAGE, "您已领过他人推荐的优惠礼包，还未使用，不能再领！");
				}
			}else{
				flag = false;
				map.put(Utils.RESULT, Utils.RESULT_RC_CHECK_FAIL);
				map.put(Utils.MESSAGE, "您已领过该用户推荐的优惠礼包，还未使用，不能再领！");
			}
		}else{
			flag = false;
			map.put(Utils.RESULT, Utils.RESULT_RC_CHECK_FAIL);
			map.put(Utils.MESSAGE, "您是e点到家老朋友了，请到客户端首页，推荐邀请新朋友，会有意想不到惊喜哦！邀请朋友越多，惊喜越多！");
		}
		
		return flag;
	}
	
	/**
	 * 新用户点击领取优惠券
	 * @param params
	 * @param map
	 */
	@Transactional
	@Override
	public void receiveCoupons(Map<String, String> params,
			Map<String, Object> map) {
		String recommend_mobile = params.get("recommend_mobile");
		String current_mobile = params.get("current_mobile");
		String city_id = params.get("city_id");
		String act_type = params.get("act_type");
		ApiT_recommend_coupon recommendCoupon = new ApiT_recommend_coupon();
		recommendCoupon.setRecommend_mobile(recommend_mobile);
		recommendCoupon.setCurrent_mobile(current_mobile);
		recommendCoupon.setCity_id(Integer.valueOf(city_id));
		recommendCoupon.setAct_type(Integer.valueOf(act_type));
		recommendCoupon.setIs_invite_send(0);
		recommendCoupon.setInsert_time(new Date());
		apiT_recommend_couponMapper.insertT_recommend_coupon(recommendCoupon);
		map.put(Utils.RESULT, Utils.RESULT_RC_SUCCESS);
		map.put(Utils.MESSAGE, "成功领取优惠券！请登录到客户端查看！");
	}
	
	/**
	 * 新注册用户如果是被推荐的 领取优惠券
	 * @param u_id
	 */
	@Override
	public void newLoginToTakeCoupons(String u_id) {
		//判断是否是推荐用户----
		ApiT_recommend_coupon recommendCoupon = apiT_recommend_couponMapper.findByCurrent(u_id);
		if(recommendCoupon!=null){//是推荐用户---要领取优惠券
			//查询当前要领取的优惠券
			Integer city_id = recommendCoupon.getCity_id();
			Integer act_type = recommendCoupon.getAct_type();
			ApiT_coupon_act act = apiT_coupon_actMapper.findByActtypeAndCityid(act_type+"", city_id+"");
			if(act!=null){//该站点存在推荐有奖活动
				//被邀请人优惠券
				String being_invited = act.getBeing_invited();
				if(!Util.isNull(being_invited)){
					String[] being_invitedArray = being_invited.split(",");
					if(being_invitedArray!=null && being_invitedArray.length>0){
						List<ApiJuan> list = new ArrayList<ApiJuan>();
						for(String cid : being_invitedArray){
							if(!Util.isNull(cid)){
								Integer j_id = Integer.valueOf(cid);
								ApiJuan juan = apiJuanMapper.selectJuanByCid(j_id);
								list.add(juan);
								//将这些优惠券放到用户信息里
								//先判断数据库是否已存在该优惠券---存在，数量加1，不存在插入
								ApiT_juan_tj juan_tj = apiJuanTJMapper.selectApiT_juan_tjByUidAndJid(u_id, j_id);
								if(juan_tj!=null){//已存在
									Integer count = juan_tj.getCount();
									count = count+1;
									juan_tj.setCount(count);
									juan_tj.setEnable(0);//未使用
									apiJuanTJMapper.updateApiT_juan_tj(juan_tj);
								}else{
									//直接插入
									apiJuanMapper.receiveJuan(u_id, cid);
								}
							}
						}
					}
				}
			}
		}
	}
	
	/**
	 * 新用户第一次下单，订单完成，给推荐人送优惠券
	 * @param u_id
	 */
	@Override
	public void newLoginFirstOrder(String u_id) {
		//判断用户是否是推荐用户
		ApiT_recommend_coupon recommendCoupon = apiT_recommend_couponMapper.findByCurrent(u_id);
		if(recommendCoupon!=null){//是推荐用户--给推荐人送优惠券
			//判断用户已完成的订单数量---不包括充值(可以只充值1元---金额较少---送优惠券不合适)---充值有礼算
			//判断推荐人优惠券是否已送出
			Integer is_invite_send = recommendCoupon.getIs_invite_send();
			if(0==is_invite_send){//未送出
				String recommend_mobile = recommendCoupon.getRecommend_mobile();//推荐人id
				//查询当前要领取的优惠券
				Integer city_id = recommendCoupon.getCity_id();
				Integer act_type = recommendCoupon.getAct_type();
				ApiT_coupon_act act = apiT_coupon_actMapper.findByActtypeAndCityid(act_type+"", city_id+"");
				if(act!=null){//该站点存在推荐有奖活动
					String invite = act.getInvite();
					if(!Util.isNull(invite)){
						String[] inviteArray = invite.split(",");
						if(inviteArray!=null && inviteArray.length>0){
							List<ApiJuan> list = new ArrayList<ApiJuan>();
							for(String cid : inviteArray){
								if(!Util.isNull(cid)){
									Integer j_id = Integer.valueOf(cid);
									ApiJuan juan = apiJuanMapper.selectJuanByCid(j_id);
									list.add(juan);
									//将这些优惠券放到用户信息里
									//先判断数据库是否已存在该优惠券---存在，数量加1，不存在插入
									ApiT_juan_tj juan_tj = apiJuanTJMapper.selectApiT_juan_tjByUidAndJid(recommend_mobile, j_id);
									if(juan_tj!=null){//已存在
										Integer count = juan_tj.getCount();
										count = count+1;
										juan_tj.setCount(count);
										juan_tj.setEnable(0);//未使用
										apiJuanTJMapper.updateApiT_juan_tj(juan_tj);
									}else{
										//直接插入
										apiJuanMapper.receiveJuan(recommend_mobile, cid);
									}
								}
							}
							
							//更新推荐人是否送出优惠券标识
							recommendCoupon.setIs_invite_send(1);
							apiT_recommend_couponMapper.updateT_recommend_coupon(recommendCoupon);
						}
					}
				}
			}
			//查询要送给推荐人的优惠券
		}
	}
}
